# -*- coding:utf-8 -*-
"""
Created on Apr 12, 2011

@author: Lithuanian
"""
from algorithm import Heuristic
#from math import abs

class manhattenSumHuristics(Heuristic):
    def evaluate(self, problem_State):
        '''
        @return: sum of manhhaten distances to the closest pile for each robot 
        '''
        evaluation = 0
        for dirtPile in problem_State.dirt_locations:
            evaluation += self.calculateClosestRobot(dirtPile[0], dirtPile[1], problem_State.robots)
        return evaluation
    
    def calculateClosestRobot(self, dirt_x, dirt_y, robots):
        '''
        @param dirt_x: the horizontal location of the dirt pile
        @param dirt_y: the vertical location of the dirt pile
        @param robots: tuple of robots locations
        @return: destenation to the closest robot, and its id   
        '''
        closestRobot = None
        for robot in robots:
            dest = abs(robot[0] - dirt_x) + abs(robot[1] - dirt_y)
            if closestRobot is None:
                closestRobot = dest
                continue
            if dest < closestRobot:
                closestRobot = dest
        return closestRobot
